﻿using System;

namespace Evaluate_Arithmetic_Expression
{
    public class Stack<T>
    {
        private ListNode<T> firstNode;
        private ListNode<T> lastNode;
        private string stackName;

        public Stack(string name)
        {
            stackName = name;
        }

        public Stack()
            : this("Stack")
        {
        }

        public void Push(T insertItem)
        {
            if (IsEmpty())
                firstNode = lastNode = new ListNode<T>(insertItem);
            else
                firstNode = new ListNode<T>(insertItem, firstNode);
        }

        public T Pop()
        {
            if (IsEmpty())
                throw new EmptyListException(stackName);

            T removeItem = firstNode.Data;

            if (firstNode == lastNode)
                firstNode = lastNode = null;
            else
                firstNode = firstNode.Next;

            return removeItem;
        }

        public bool IsEmpty()
        {
            return firstNode == null;
        }

        public void Display()
        {
            if (IsEmpty())
            {
                Console.WriteLine("Empty " + stackName);
            }
            else
            {
                Console.Write("The " + stackName + " is: ");
                ListNode<T> current = firstNode;

                while (current != null)
                {
                    Console.Write(current.Data + " ");
                    current = current.Next;
                }
            }
        }
    }
}
